package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.common.LoginVo;
import org.example.common.R;
import org.example.mapper.CommentsMapper;
import org.example.mapper.PostsMapper;
import org.example.pojo.entity.*;
import org.example.pojo.entity.Comments;
import org.example.pojo.vo.CommentsVo;
import org.example.pojo.vo.CommentsVo;
import org.example.service.AttractionsService;
import org.example.service.CommentsService;
import org.example.service.PostService;
import org.example.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static org.example.utils.securityContextUserInfo.getContextUserInfo;
@Slf4j
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
        implements CommentsService {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    PostService postService;
    @Autowired
    AttractionsService attractionsService;
    @Override
    public Page<CommentsVo> pageList(Integer page, Integer size, String relateName, String delFlag) {
        Page<Comments> pages = new Page<>(page, size);
        LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(relateName)) {
            wrapper.like(Comments::getContent, relateName);
        }
        if (StringUtils.isNoneBlank(delFlag)) {
            wrapper.like(Comments::getDelFlag, delFlag);
        }
        List<CommentsVo> PostsVoList = new ArrayList<>();
        Page<Comments> PostsPage = baseMapper.selectPage(pages, wrapper);
        PostsPage.getRecords().forEach(Comments -> {
            CommentsVo commentsVo = new CommentsVo();
            SysUser user = sysUserService.getById(Comments.getCreateId());
            if(1 == Comments.getRelateType()){
                Posts posts = postService.getById(Comments.getRelateId());
                commentsVo.setRelateName(posts.getTitle());
            }
            if(2 == Comments.getRelateType()){
                Attractions attractions = attractionsService.getById(Comments.getRelateId());
                log.info("attractions:{}",attractions);
                    commentsVo.setRelateName(attractions.getScenicName());
            }
            String userName = user.getUserName();

            BeanUtils.copyProperties(Comments, commentsVo);
            commentsVo.setUserName(userName);
            PostsVoList.add(commentsVo);
        });
        Page<CommentsVo> commentsVoPage = new Page<>();
        commentsVoPage.setRecords(PostsVoList);
        commentsVoPage.setPages(PostsPage.getPages());
        commentsVoPage.setSize(PostsPage.getSize());
        commentsVoPage.setTotal(PostsPage.getTotal());
        return commentsVoPage;

    }

    @Override
    public R getCommentsDetail(String scenicId) {
        return R.ok(this.getById(scenicId)) ;
    }

    @Override
    public R addComments(Comments comments) {
        LoginVo userInfo = getContextUserInfo();
        comments.setCreateTime(LocalDateTime.now());
        comments.setUpdateTime(LocalDateTime.now());
        comments.setDelFlag("0");
        comments.setCreateId(Integer.valueOf(userInfo.getUserId()));
        //保存评论基本信息
        this.save(comments);
        return R.ok("操作成功");
    }
    @Override
    public R updateComments(Comments Comments) {
        /*LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comments::getScenicName, Comments.getScenicName());
        Comments PostsOne = this.getOne(wrapper);
        if (PostsOne != null) {
            return R.error(StatusCode.ATTR_ALREADY_EXIST);
        }*/
        Comments.setUpdateTime(LocalDateTime.now());
        //更新评论基本信息
        this.updateById(Comments);
        return R.ok("操作成功");
    }

    @Override
    public void removeCommentsById(String id) {
        Comments comments = new Comments();
        comments.setDelFlag("1");
        comments.setId(Integer.valueOf(id));
        this.updateById(comments);
    }
}
